package test.ant.com.myframe;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import test.ant.com.myframe.base.AntBaseActivity;
import test.ant.com.myframe.base.AntBaseFragment;
import test.ant.com.myframe.fragments.Fragment001;
import test.ant.com.myframe.fragments.Fragment002;
import test.ant.com.myframe.utils.AntLog;

public class MainActivity extends AntBaseActivity {
    private ViewPager pager;
    private RadioGroup radioGroup;
    private List<AntBaseFragment> list = new ArrayList<>();
    private int[] ids = {R.id.ac_main_rb_001, R.id.ac_main_rb_002,
            R.id.ac_main_rb_003, R.id.ac_main_rb_004,
            R.id.ac_main_rb_005};


    @Override
    public int getContextView() {
        return R.layout.ac_main;
    }

    @Override
    public void initView() {
        pager = (ViewPager) findViewById(R.id.ac_main_vp00_001);
        radioGroup = (RadioGroup) findViewById(R.id.ac_main_rg_001);
    }

    @Override
    public void initData() {
        Fragment001 fragment;

        for (int i = 0; i < 4; i++) {
            fragment = new Fragment001();
            fragment.setText(i + " str");
            list.add(fragment);
        }
        list.add(1, new Fragment002());


        //
        Mypager adapter = new Mypager(getSupportFragmentManager(), list);
        pager.setAdapter(adapter);

    }

    @Override
    public void initListener() {
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                AntLog.i(TAG, "position==" + position);
                radioGroup.check(ids[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int viewId) {
                AntLog.i(TAG, "index==" + viewId);
                switch (viewId) {
                    case R.id.ac_main_rb_001:
                        pager.setCurrentItem(0);
                        break;
                    case R.id.ac_main_rb_002:
                        pager.setCurrentItem(1);
                        break;
                    case R.id.ac_main_rb_003:
                        pager.setCurrentItem(2);
                        break;
                    case R.id.ac_main_rb_004:
                        pager.setCurrentItem(3);
                        break;
                    case R.id.ac_main_rb_005:
                        pager.setCurrentItem(4);
                        break;

                }
            }
        });
    }


    /***********************************************************************/

    class Mypager extends FragmentPagerAdapter {
        private List<AntBaseFragment> flist;
        FragmentManager fm;


        public Mypager(FragmentManager fm, List<AntBaseFragment> flist) {
            super(fm);
            this.fm = fm;
            this.flist = flist;
        }

        @Override
        public Fragment getItem(int position) {
            return flist.get(position);
        }

        @Override
        public int getCount() {
            return flist == null ? 0 : flist.size();
        }
    }

}
